package com.springboot.springbootlogindemo.utils;

import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.*;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Geo {
    public static String Geo_1(String ip) throws IOException, GeoIp2Exception {
        String destFilePath = new File("GeoLite2-City.mmdb").getAbsolutePath();
        File database = new File(destFilePath);
        // 读取数据库内容
        DatabaseReader reader = new DatabaseReader.Builder(database).build();
        InetAddress ipAddress = InetAddress.getByName(ip);//要解析的ip地址//112.10.180.1//121.43.161.169
        // 获取查询结果
        CityResponse response = reader.city(ipAddress);
        // 获取国家信息
        Country country = response.getCountry();
        // 'CN'
        // 获取省份
        Subdivision subdivision = response.getMostSpecificSubdivision();
        // 获取城市
        City city = response.getCity();
        Postal postal = response.getPostal();
        Location location = response.getLocation();
        String address=location.getLatitude()+" "+location.getLongitude();
        return address;
    }
    /**
     * 腾讯API 逆地址解析  根据经纬度得到地址信息

     * @return
     */
    public static String getAddress(String lat,String lng,String key) {
        //lat 小  log  大

        //纬度前 经度后 "https://apis.map.qq.com/ws/geocoder/v1/?location="+lat+","+lng+"&key="+key+"&get_poi=1";
        String urlString = "https://apis.map.qq.com/ws/geocoder/v1/?location="+lat+","+lng+"&key="+key;
        String res = "";
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line + "\n";
            }
            in.close();
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        //System.out.println(res);
        return res;
    }

    /**
     * 腾讯API 坐标转换

     * @return
     */
    public static String changeAddress(String lat,String lng) {
        //lat 小  log  大
//        String []arr=address.split("\\s+");
//        String lat=arr[0];
//        String lng=arr[1];
        String urlString = "https://apis.map.qq.com/ws/coord/v1/translate?locations="+lat+","+lng+"&type=1&key="+"XS3BZ-2MGWG-W4ZQN-QSFMX-NJGG7-WFBHQ";
        String res = "";
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("GET");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line + "\n";
            }
            in.close();
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        //System.out.println(res);
        return res;
    }

    public static void main(String[] args) throws IOException, GeoIp2Exception {
        String address=Geo_1("60.191.122.35");
        String []arr=address.split("\\s+");
        String lat=arr[0];
        String lng=arr[1];
        String change=changeAddress(lat,lng);
        System.out.println(Geo_1("60.191.122.35")+change);
    }
}
